<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Wechat\BaseController;
use App\Libraries\classs\Prizes;
use App\Models\PrizeRank;
use App\Libraries\classs\Face;
use App\Models\User\Prize\Logs as Logs;
use App\Models\Prize as PrizeModel;


class DrawController extends BaseController
{
    //
    public function index(){
      return view('wechat.draw.index');
    }

    public function  rand(Request $request){
        if($request->ajax()){
            $face_count =  Face::getInstance()->getFaceCount($this->_uid);
            if(!$face_count){
                return response()->json(array('success'=>false,'msg'=>'每投一次,可抽奖一次,每天有三次机会!','url'=>url('wechat/build/index')));
            }elseif ($face_count > 3){
                return response()->json(array('success'=>false,'mgs'=>'你今天的3次抽奖机会已用完，明天可以继续刷脸投票进行抽奖哦'));
            }
            $count = Logs::where('x_uid','=',$this->_uid)->where('created_at','>=',date('Y-m-d'))->count();
            if($count <= 3) {
                 if($face_count - $count <= 0){
                     return response()->json(array('success'=>false,'msg'=>'每投一次,可抽奖一次,每天有三次机会!','url'=>url('wechat/build/index')));
                 }
                $result = Prizes::getInstance()->getPrizeRand($this->_uid);
                if($result['success'] == true){
                    $url = $result['data']->seven?"wechat/mine":"";
                    return response()->json(array('success' => true, 'data' => $result,'url'=>url($url)));
                }
                return response()->json(array('success' => false, 'msg' => $result['msg']));
            }
            return response()->json(array('success'=>false,'msg'=>'你今天的3次抽奖机会已用完，明天可以继续刷脸投票进行抽奖哦'));
        }
        return response()->json(array('success'=>false,'msg'=>'请求错误'));
    }


    public function submit(Request $request){
          if($request->ajax()){
              $count = Logs::where('x_uid','=',$this->_uid)->where('created_at','>=',date('Y-m-d'))->count();
              if($count <=3){
                  $rid = $request->rid;
                  $rand_data=array(
                      'x_uid'=>$this->_uid,
                      'rank_id'=>$rid,
                      'prize_id'=>0,
                      'status'=>1,
                  );
                  $rank = PrizeRank::find($rid);
                  if($rank){
                      if($rank->seven == 0){
                          $rand_data['status']=-1;
                      }
                      if(Logs::create($rand_data)){
                          return response()->json(array('success'=>true,'data'=>$rank));
                      }
                  }
              }else{
                  return response()->json(array('success'=>false,'mgs'=>'你今天的3次抽奖机会已用完，明天可以继续刷脸投票进行抽奖哦'));
              }
          }
        return response()->json(array('success'=>false));
    }

    public function prize(){
        $PrizeRand = PrizeRank::where('seven','=',0)->first();
        $prize = PrizeModel::with('attach')->where('rank_id','!=',$PrizeRand->id)->orderBy('rank_id','asc')->get();
        $data['prize'] =$prize;
        $user_prize_logs_count = Logs::where('status','>',0)->count();
        $data['prize_count']= $user_prize_logs_count;
        return view('wechat.draw.prize',array('data'=>$data));
    }

    public function prizeLog(Request $requests){
        if($requests->ajax()){
            $prizeLog = Logs::with(array('prize'=>function($query){
                $query->select('id','name');
            },'Users'=>function($query){
                $query->select('id','nickname','headimgurl');
            }))->where('status','=',1)->take(5)->get();
            return response()->json(array('success'=>true,'volelog'=>$prizeLog));
        }
        return response()->json(array('success'=>false));
    }
}
